$package('jeecg.TostPjDepartpushmoney');

$("#projectselect").ready(function (){
	$.ajax({
		url:"getProject.do",
		dataType:'json',
		success:function (result){
			var str="<select name='id' id='pjId'><option></option>";
			for(var i=0;i<result.length;i++){
				str+="<option value='"+result[i].id+"'>"+result[i].pjProjectname+"</option>";
			}
			str+="</select>";
			document.getElementById("projectselect").innerHTML=str;
		}
	});
})

//实现点击编辑函数
	  	function fun(obj,ary){
  		var value = obj.innerHTML;
  		var parent = obj.parentNode;
  		var col = parent.colSpan;
  		if(col>=2&&col<=4){
  			parent.colSpan=1;
  			var par = obj.parentNode.parentNode;
  			var ce = parent.cellIndex;
  			var cellr = par.cells[ce-1];
  			if(cellr.style.display=="none"){
  				cellr.style.display="inline";
  				cellr.innerHTML="<input id='in1' type = 'text' value='+' style='width:50%' onblur='fun1(this,\""+ary+"\")'>";
  			}else{
  				var cell = par.insertCell(ce);
  				cell.innerHTML="<input id='in1' type = 'text' value='+' style='width:50%' onblur='fun1(this,\""+ary+"\")'>";
  			}
  			parent.innerHTML = "<input id='in2' type = 'text' value='"+value+"' style='width:90%' onblur='fun1(this,\""+ary+"\")'>";
  			document.getElementById("in1").focus();
  		}
  		else {
	  		parent.innerHTML = "<input id='in2' type = 'text' value='"+value+"' style='width:90%' onblur='fun1(this,\""+ary+"\")'>";
	  		document.getElementById("in2").focus();
	  	}
  	}
  	function fun1(obj,ary){
  		var va1 = obj.value;
  		var parent = obj.parentNode;
  		var ce = parent.cellIndex;
  		var roindex = parent.parentNode.rowIndex;
  		if(roindex==4){
  			if(ce>=1&&ce<=4){
  				var stbp = new Array;
  				var oldValue = document.getElementById("pdyBpextractmoney").value;
  	  			if(oldValue==null||oldValue==undefined||oldValue==""){
  	  				oldValue=ary;
  	  				stbp = oldValue.split(",");
  	  			}else{
  	  				stbp = oldValue.split("|");
  	  			}
	  	  		switch(ce){
	  	  		case 1:document.getElementById("pdyBpextractmoney").value=va1+"|"+stbp[1]+"|"+stbp[2]+"|"+stbp[3];
	  	  			break;
	  	  		case 2:document.getElementById("pdyBpextractmoney").value=stbp[0]+"|"+va1+"|"+stbp[2]+"|"+stbp[3];
	  	  			break;
	  	  		case 3:document.getElementById("pdyBpextractmoney").value=stbp[0]+"|"+stbp[1]+"|"+va1+"|"+stbp[3];
	  	  			break;
	  	  		case 4:document.getElementById("pdyBpextractmoney").value=stbp[0]+"|"+stbp[1]+"|"+stbp[2]+"|"+va1;
	  	  			break;
	  	  		}
  			}
  			if(ce>=5&&ce<=6){
  				var stbp = new Array;
  				var oldValue = document.getElementById("pdyApextractmoney").value;
  	  			if(oldValue==null||oldValue==undefined||oldValue==""){
  	  				oldValue=ary;
  	  				stbp = oldValue.split(",");
  	  			}else{
  	  				stbp = oldValue.split("|");
  	  			}
  	  			switch(ce){
	  	  		case 5:document.getElementById("pdyApextractmoney").value=va1+"|"+stbp[1];
		  			break;
		  		case 6:document.getElementById("pdyApextractmoney").value=stbp[0]+"|"+va1;
		  			break;
  	  			}
  			}
  		}else if(roindex==5){
  			if(ce>=1&&ce<=4){
  				var stbp = new Array;
  				var oldValue = document.getElementById("pdyBpextracttime").value;
  	  			if(oldValue==null||oldValue==undefined||oldValue==""){
  	  				oldValue=ary;
  	  				stbp = oldValue.split(",");
  	  			}else{
  	  				stbp = oldValue.split("|");
  	  			}
	  	  		switch(ce){
	  			case 1:document.getElementById("pdyBpextracttime").value=va1+"|"+stbp[1]+"|"+stbp[2]+"|"+stbp[3];
		  			break;
		  		case 2:document.getElementById("pdyBpextracttime").value=stbp[0]+"|"+va1+"|"+stbp[2]+"|"+stbp[3];
		  			break;
		  		case 3:document.getElementById("pdyBpextracttime").value=stbp[0]+"|"+stbp[1]+"|"+va1+"|"+stbp[3];
		  			break;
		  		case 4:document.getElementById("pdyBpextracttime").value=stbp[0]+"|"+stbp[1]+"|"+stbp[2]+"|"+va1;
		  			break;
	  	  		}
  			}
  			if(ce>=5&&ce<=6){
  				var stbp = new Array;
  				var oldValue = document.getElementById("pdyApextracttime").value;
  	  			if(oldValue==null||oldValue==undefined||oldValue==""){
  	  				oldValue=ary;
  	  				stbp = oldValue.split(",");
  	  			}else{
  	  				stbp = oldValue.split("|");
  	  			}
  	  			switch(ce){
	  			case 5:document.getElementById("pdyApextracttime").value=va1+"|"+stbp[1];
	  				break;
	  			case 6:document.getElementById("pdyApextracttime").value=stbp[0]+"|"+va1;
	  				break;
  	  			}
  			}
  		}else if(roindex==6){
  			document.getElementById("pdyDesc").value=va1;
  		}
  		if(va1==""||va1=="undefined"||va1==null){
	  		va1="0";
	  	}
  		if(va1=="+"){
  			var par = obj.parentNode.parentNode;
	  		par.cells[ce].style.display= "none";
	  		par.cells[ce+1].colSpan=2;
  		}
  		parent.innerHTML="<span onclick='fun(this)'>"+va1+"</span>";
  		
  	}

  	
  	//部门id Name设值。
  	function setHapNameValue(){
		var selectIndex = document.getElementById("depart").selectedIndex;
		var selectText = document.getElementById("depart").options[selectIndex].text;
		var selectvalue = document.getElementById("depart").options[selectIndex].value;
		document.getElementById("hdpName").value=selectText;
		document.getElementById("hdpId").value=selectvalue;
	}
 
	
	//下面拼接jsp要的数据
	var depart = new Array;//部门
	var pdytype = new Array;//类型
	var databp = new Array;//基础提成节点常量数据
	var dataap = new Array;//奖励提成节点常量数据
	
	$.ajax({
		url:urls['msUrl']+"/tostHrBaseinfo/queryDepartId.do",
		dataType:'json',
		async:false,
		success:function(result){
			depart = result;//部门
		}
	});
	
	$.ajax({
		url:"getBuildType.do",
		dataType:'json',
		async:false,
		success:function(result){
			pdytype = result;//类型
		}
	});
	
	$.ajax({
		url:"getAppoint.do",
		dataType:'json',
		async:false,
		success:function(result){
			dataap = result;//奖励提成节点常量数据
		}
	});
	
	$.ajax({
		url:"getBppoint.do",
		dataType:'json',
		async:false,
		success:function(result){
			databp = result;//基础提成节点常量数据
		}
	});

	loadData(depart,pdytype,databp,dataap);//把数据传到jsp里的js函数里。
	

$(document).ready(function (){
	$("#btn-search").click(function (){
		var pjId = document.getElementById("pjId").value;
		$.ajax({
			url:"queryByPjId.do",
			dataType:'json',
			data:[{name:'pjId',value:pjId}],
			success:function (result){
				
				var str="";
				for(var i=0;i<result.length;i++){
					str+="<tr class='tabhead'><td colspan='9' align='center'>"+result[i].pjName+"</td></tr>";
					str+="<tr><td colspan='3' align='center'></td>" +
							"<td colspan='4' align='center'>基础提成</td>" +
							"<td colspan='2' align='center'>奖励提成</td></tr>";
					
					str+="<tr ><td width='15%' align='center'>部门</td>" +
						 "<td width='10%' align='center'>类型</td>" +
						 "<td width='25%' align='center'>提成节点</td>";
					var stbp = new Array;
					var stap = new Array;
					stap = result[i].pdyAppoint.split("|");
					stbp = result[i].pdyBppoint.split("|");
					for(var j=0;j<=3;j++){
						str += "<td width='10%' align='center'><select  id='td_"+i+"_"+j+"' class='easyui-combobox' style='width:95%; border: 1px solid #ccc'> </select></td>";
					}
					var f = 5;
					for(var l=0;l<=1;l++){
						str += "<td width='10%' align='center'><select  id='td_"+i+"_"+f+"' class='easyui-combobox' style='width:95%; border: 1px solid #ccc'> </select></td>";
						f++;
					}
				str += "</tr>";
					
				str += "<tr><td rowspan='4' align='center'><select id='hdpId_"+i+"' onchange='onHdpId("+i+")'>";
				
				for(var t=0;t<depart.length;t++){
					var st = "";
					if(result[i].hdpId==depart[t].id)
						st = "selected='selected'";
					str += "<option value='"+depart[t].id+"' "+st+">"+depart[t].hdpName+"</option>";
				}
				str += "</select></td>";
		
				str += "<td rowspan='4' align='center'><select id='pdyType_"+i+"' onchange='onHdpType("+i+")'>";
				
				for(var r=0;r<pdytype.length;r++){
					var st1="";
					if(parseInt(result[i].pdyType)==parseInt(pdytype[r].paValue)){
						st1 = "selected='selected'";
					}
					str += "<option value='"+pdytype[r].paValue+"' "+st1+">"+pdytype[r].paName+"</option>";
				}
				str += "</select></td>";
					str += "<td align='center'>是否提取</td>";
					var stbp1 = new Array;
					var stap1 = new Array;
					stbp1 = result[i].pdyBpisextract.split("|");
					stap1 = result[i].pdyApisextract.split("|");
					for(var u=0;u<=3;u++){
						str += "<td align='center'>";
						if(stbp1[u]=="true")
							str += "<select id='bpis_"+i+"_"+u+"' onchange='onBpIs("+i+","+u+",\""+result[i].pdyBpisextract+"\")'> <option value='true' selected='selected' >是</option>" +
									"<option value='false' >否</option></select>";
						else if(stbp1[u]=="false")
							str += "<select id='bpis_"+i+"_"+u+"' onchange='onBpIs("+i+","+u+",\""+result[i].pdyBpisextract+"\")'> <option value='true'>是</option>" +
									"<option value='false' selected='selected'>否</option></select>";
						else
							str += "<span>---</span>";
						str += "</td>";
					}
					
					for(var v=0;v<=1;v++){
						str += "<td align='center'>";
						if(stap1[v]=="true"){
							str+="<select id='apis_"+i+"_"+v+"' onchange='onApIs("+i+","+v+",\""+result[i].pdyApisextract+"\")'> <option value='true' selected='selected' >是</option>" +
							"<option value='false' >否</option></select>";
						}
						else if(stap1[v]=="false")
							str+="<select id='apis_"+i+"_"+v+"' onchange='onApIs("+i+","+v+",\""+result[i].pdyApisextract+"\")'> <option value='true'>是</option>" +
							"<option value='false' selected='selected'>否</option></select>";
						else
							str += "<span>---</span>";
						str+="</td>";
					}
					
					str+="<tr><td align='center'>提取金额</td>";
					
					var stbp2 = new Array;
					var stap2 = new Array;
					stbp2 = result[i].pdyBpextractmoney.split("|");
					stap2 = result[i].pdyApextractmoney.split("|");
					for(var k=0;k<=3;k++){
						if(stbp2[k]=="undefined"||stbp2[k]=="")
							stbp2[k]="0.0";
						if(stbp2[k]=="+"){
							k+=1;
							str += "<td align='center' colspan='2'><span onclick='fun(this,\""+stbp2+"\")'>"+stbp2[k]+"</span></td>";
							continue;
						}
						str += "<td align='center'><span onclick='fun(this,\""+stbp2+"\")'>"+stbp2[k]+"</span></td>";
					}
					for(var m=0;m<=1;m++){
						if(stap2[m]=="undefined"||stap2[m]=="")
							stap2[m]="0.0";
						if(stap2[m]=="+"){
							m+=1;
							str += "<td align='center' colspan='2'><span onclick='fun(this,\""+stap2+"\")'>"+stap2[m]+"</span></td>";
							continue;
						}
						str += "<td align='center'><span onclick='fun(this,\""+stap2+"\")'>"+stap2[m]+"</span></td>";
					}
					
					str += "</tr>";
					
					var stbp3 = new Array; 
					var stap3 = new Array;
					stap3 = result[i].pdyApextracttime.split("|");
					stbp3 = result[i].pdyBpextracttime.split("|");
					str+="<tr><td align='center'>提取时间</td>";
					for(var n=0;n<=3;n++){
						if(stbp3[n]=="undefined"||stbp3[n]=="")
							stbp3[n]="---";
						str += "<td align='center'><span onclick='fun(this,\""+stbp3+"\")'>"+stbp3[n]+"</span></td>";
					}
					for(var p=0;p<=1;p++){
						if(stap3[p]=="undefined"||stap3[p]=="")
							stap3[p]="---";
						str += "<td align='center'><span onclick='fun(this,\""+stap3+"\")'>"+stap3[p]+"</span></td>";
					}
						str += "</tr>";
					str+="<tr><td align='center'>备注</td>" +
						"<td colspan='5' align='center'>" +
						"<span onclick='fun(this,\""+result[i].pdyDesc+"\")'>"+result[i].pdyDesc+"</span>" +
						"</td><td align='center'><input type='button' value='保存' " +
						"onclick='subSave("+result[i].id+","+result[i].pjId+",\""+result[i].pjName+"\")' />" +
						"<input type='button' value='删除' onclick='del("+result[i].id+")'/></td></tr>";
				}
				document.getElementById("data-list").innerHTML=str;
				
				
				var d=0;
				while(d<result.length){
					for(var a=0;a<=3;a++){
						var stbp = new Array;
						stbp = result[d].pdyBppoint.split("|");
						$("#td_"+d+"_"+a).combobox({
							data:databp,
							cache: true,  //tostHrDepartinfo/getDepartBypost  tostHrBaseinfo
							  // panelHeight: 'auto',//自动高度适合    去掉，显示滚动条
							dataType:'json',
							valueField:'id',
							textField:'paName',
							multiple:false,
							async:false,
							formatter:function(resultf){
								var s= "<span><input type='hidden' class='selectId' style='vertical-align: middle' id='selectId_"+resultf.id+"' value='"+resultf.paName+"'>"+resultf.paName+"</span>";
								return s;
							},
							onSelect:function(resultf){
								var id = $(this).attr("id");
								var index = parseInt(id.substr(id.length-1,1));
								var idIndex = parseInt(id.substr(3,1));
								var oldValue = document.getElementById("pdyBppoint").value;
								if(oldValue==""){
									oldValue = result[idIndex].pdyBppoint;
								}
								var stbpEdit = oldValue.split("|");
								switch(index){
								case 0:document.getElementById("pdyBppoint").value=resultf.paName+"|"+stbpEdit[1]+"|"+stbpEdit[2]+"|"+stbpEdit[3];
									break;
								case 1:document.getElementById("pdyBppoint").value=stbpEdit[0]+"|"+resultf.paName+"|"+stbpEdit[2]+"|"+stbpEdit[3];
									break;
								case 2:document.getElementById("pdyBppoint").value=stbpEdit[0]+"|"+stbpEdit[1]+"|"+resultf.paName+"|"+stbpEdit[3];
									break;
								case 3:document.getElementById("pdyBppoint").value=stbpEdit[0]+"|"+stbpEdit[1]+"|"+stbpEdit[2]+"|"+resultf.paName;
									break;
								}
								$("#selectId_"+result.id).attr("checked", true);
							},
							onUnselect:function(result){
								$("#selectId_"+result.id).attr("checked", false);
							},
							onLoadSuccess:function(){
								$("#td_"+d+"_"+a).combobox('select',stbp[a]);
							},
						});
					}
					
					var g = 0;
					for(var c=5;c<=6;c++){
						var stap = new Array;
						stap = result[d].pdyAppoint.split("|");
						$("#td_"+d+"_"+c).combobox({
							data:dataap,
							cache: true,  //tostHrDepartinfo/getDepartBypost  tostHrBaseinfo
						   // panelHeight: 'auto',//自动高度适合    去掉，显示滚动条
							dataType:'json',
							valueField:'id',
							textField:'paName',
							multiple:false,
							async:false,
							formatter:function(result5){
								 var s= "<span> <input type='hidden' class='selectId' style='vertical-align: middle' id='selectId_"+result5.id+"' value='"+result5.paName+"'>"+result5.paName+"</span>";
								 return s;
							},
							onSelect:function(result6){
								var id = $(this).attr("id");
								var index = parseInt(id.substr(id.length-1,1));
								var idIndex = parseInt(id.substr(3,1));
								var oldValue = document.getElementById("pdyAppoint").value;
								if(oldValue==""){
									oldValue = result[idIndex].pdyAppoint;
								}
								var stapEdit = oldValue.split("|");
								switch(index){
								case 5:document.getElementById("pdyAppoint").value=result6.paName+"|"+stapEdit[1];
									break;
								case 6:document.getElementById("pdyAppoint").value=stapEdit[0]+"|"+result6.paName;
									break;
								}
								$("#selectId_"+result6.id).attr("checked", true);
							},
							onUnselect:function(result7){
								$("#selectId_"+result7.id).attr("checked", false);
							},
							onLoadSuccess:function(){
								$("#td_"+d+"_"+c).combobox('select',stap[g]);
							}
						});
						g++;
					}
					d++;
				}
			}
		});
	});
})